package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.Purchase;
import com.ggk911.pharmacy_system.service.impl.PurchaseServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 采购单控制层
 *
 * @author ggk-911
 * @version 1.0 2023/3/20 9:48
 */
@RestController
@CrossOrigin
@RequiredArgsConstructor
@Slf4j
@Api(tags = "采购单控制层")
@RequestMapping("/purchase")
public class PurchaseController {
    private final PurchaseServiceImpl service;

    //GET查询
    @GetMapping(value = "/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               Integer searchId,
                               Integer userId,
                               String purchaseStatus) {
        Page<Purchase> purchasePage = new Page<>(page, size);
        QueryWrapper<Purchase> wrapper = new QueryWrapper<>();

        wrapper.like(searchId != null, "purchase_id", searchId);
        wrapper.like(userId != null, "user_id", userId);
        wrapper.like(purchaseStatus != null, "purchase_status", purchaseStatus);

        service.page(purchasePage, wrapper);
        return RestResult.ok("查询成功", purchasePage);
    }

    //PUT添加
    @PutMapping("/insert")
    public RestResult insert(Purchase purchase) {
        if (service.save(purchase)) {
            return RestResult.ok("添加成功，返回订单编号为：" + purchase.getPurchaseId(), purchase.getPurchaseId());
        }
        return RestResult.error("添加失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer id) {
        if (service.removeById(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }

    //POST修改
    @PostMapping("/update")
    public RestResult update(Purchase purchase) {
        if (service.updateById(purchase)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }
}
